package com.example.administrator.yuejian_2.fragment;


<<<<<<< HEAD
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.adapter.ManswerAdapter;
import com.example.administrator.yuejian_2.bean.MasterAnswerAddBean;
import com.example.administrator.yuejian_2.bean.MasterAnswerBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.MasterService;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
=======
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.yuejian_2.R;
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2


/**
 * A simple {@link Fragment} subclass.
 */
public class MaAnswerFragment extends Fragment {
<<<<<<< HEAD
    private RecyclerView recyclerView;
    private ManswerAdapter adapter;
    private int mid;
    private int an_uid;
    private String an_ques;
    private EditText ques;
    private Button commitButton;
=======
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2

    public MaAnswerFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_ma_answer, container, false);
    }

<<<<<<< HEAD
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mid = getActivity().getIntent().getIntExtra("mid",-1);

        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        an_uid = sharedPreferences.getInt("user_id",0);

        recyclerView = view.findViewById(R.id.manswer_recyclerView);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MasterService service = retrofit.create(MasterService.class);
        Call<List<MasterAnswerBean>> call = service.maanswer(mid);
        call.enqueue(new Callback<List<MasterAnswerBean>>() {
            @Override
            public void onResponse(Call<List<MasterAnswerBean>> call, Response<List<MasterAnswerBean>> response) {
                if (getActivity() == null)
                    return;
                adapter = new ManswerAdapter(getActivity(),response.body());
                LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setAdapter(adapter);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
            }

            @Override
            public void onFailure(Call<List<MasterAnswerBean>> call, Throwable t) {

            }
        });

        ques = view.findViewById(R.id.manswer_ques);
        commitButton = view.findViewById(R.id.button2);
        commitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                an_ques = ques.getText().toString();
                answeradd();

            }
        });



    }

    private void answeradd(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MasterService service = retrofit.create(MasterService.class);
        Call<MasterAnswerAddBean> call = service.answeradd(mid,an_uid,an_ques);
        call.enqueue(new Callback<MasterAnswerAddBean>() {
            @Override
            public void onResponse(Call<MasterAnswerAddBean> call, Response<MasterAnswerAddBean> response) {
                if (response.body().isInfo() == true){
                    Toast.makeText(getActivity(),"提交成功",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<MasterAnswerAddBean> call, Throwable t) {

            }
        });
    }
=======
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
}
